package com.eqdd.yiqidian.ui.discover.friendcircle;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.eqdd.yiqidian.FriendCircleInfoActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.WorkCircleBean;
import com.eqdd.yiqidian.bean.rebuild.CommentResultBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.ui.discover.workcircle.ZanListActivity;
import com.eqdd.yiqidian.ui.mine.UserCardActivity;
import com.eqdd.yiqidian.utils.DialogUtil;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.eqdd.yiqidian.widget.CustomLinkMovementMethod;
import com.eqdd.yiqidian.widget.InputDialogFragment;
import com.jakewharton.rxbinding.view.RxView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import org.reactivestreams.Publisher;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.rong.imkit.utils.RongDateUtils;

public class FriendCircleInfoActivity extends BaseActivity {
    private static final String TAG = "FriendCircleInfoActivity";
    private FriendCircleInfoActivityCustom dataBinding;
    private WorkCircleBean workBean;
    private SlimAdapterEx slimAdapterExHead;
    private SlimAdapterEx slimAdapterExImg;
    private InputDialogFragment inputDialogFragment;
    private MVCCoolHelper<List<CommentResultBean>> mvcHelper;
    private ModelRx2DataSource<CommentResultBean> dataSource;
    private SlimAdapterEx<CommentResultBean> slimAdapterExComment;
    private String createId;
    private SimpleDateFormat sf;
    private List<CommentResultBean> commentBeans;
    private AlertDialog deleteArticle;
    private ActionSheetDialog alertView;
    private AlertDialog deleteComment;
    private ActionSheetDialog alertViewDelete;
    private Map<SlimAdapterEx, CommentResultBean> slimMap = new HashMap<>();
    private int commentId;
    private int clickedPos;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        isKeyBoardJustPan = false;
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.friend_work_circle_info);
        initTopTitleBar(View.VISIBLE, "详情");
//        initTopRightText("所有评论", v -> {
//            Bundle bundle = new Bundle();
//            bundle.putString("workCircleId", workBean.getId() + "");
//            bundle.putString("staffName", workBean.getStaffName());
//            bundle.putString(Config.GUID, workBean.getCreater());
//            startActivityForResult(WorkCircleCommentActivity.class, bundle, Config.WORK_CIRCLE_COMMENT);
//        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        setView();
    }


    @Override
    public void initData() {
//        if (slimAdapterExImg == null) {
//            initInputDialog();
//            sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
//            workBean = (WorkCircleBean) getIntent().getSerializableExtra("bean");
//            setRecyclerViewMoveFast(new LinearLayoutManager(this), dataBinding.recyclerViewImg);
//            dataBinding.recyclerViewImg.setLayoutManager(new GridLayoutManager(this, 3));
//            ImageView[] imageViews = new ImageView[workBean.getNewImages() == null ? 0 : workBean.getNewImages().size()];
//            slimAdapterExImg = SlimAdapterEx.create().register(R.layout.list_item_full_img, new SlimInjector<String>() {
//                @Override
//                public void onInject(String data, IViewInjector injector) {
//                    imageViews[workBean.getNewImages().indexOf(data)] = (ImageView) injector.findViewById(R.id.iv_img);
//                    injector.with(R.id.iv_img, v -> {
//                        ImageUtil.setImage(data, (ImageView) v);
//                    }).clicked(R.id.iv_img, v -> {
//                        dataBinding.vImageWatcher.setBackgroundColor(Color.parseColor("#000000"));
//                        dataBinding.vImageWatcher.show((ImageView) v, Arrays.asList(imageViews), workBean.getNewImages());
//                    });
//                }
//            }).attachTo(dataBinding.recyclerViewImg);
//            RxView.clicks(dataBinding.stvSupport).subscribe(v -> zan());
//
//            if (slimAdapterExHead == null) {
//                setRecyclerViewMoveFast(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false), dataBinding.recyclerViewHead);
//                slimAdapterExHead = SlimAdapterEx.create().register(R.layout.list_item_head_img, new SlimInjector<WorkCircleBean.UserZanBean>() {
//                    @Override
//                    public void onInject(WorkCircleBean.UserZanBean workBean, IViewInjector injector) {
//                        injector.with(R.id.iv_head_img, v -> ImageUtil.setCircleImage(workBean.getHeadImage(), (ImageView) v));
//                    }
//                }).attachTo(dataBinding.recyclerViewHead);
//            }
//            ItemClickSupport.addTo(dataBinding.recyclerViewHead)
//                    .setOnItemClickListener((recyclerView, position, v) -> {
//                        Bundle bundle = new Bundle();
//                        bundle.putString("id", workBean.getId() + "");
//                        bundle.putString("idName", "friendCircleId");
//                        startActivity(ZanListActivity.class, bundle);
//                    });
//            if (slimAdapterExComment == null) {
//                setRecyclerViewMoveFast(new LinearLayoutManager(this), dataBinding.recyclerViewComment);
//                slimAdapterExComment = SlimAdapterEx.create().register(R.layout.list_item_36, new SlimInjector<CommentResultBean>() {
//                    @Override
//                    public void onInject(CommentResultBean dataComment, IViewInjector injector) {
//                        try {
//                            injector.with(R.id.iv_comment_head, v -> ImageUtil.setCircleImage(dataComment.getIphoto(), (ImageView) v))
//                                    .text(R.id.tv_comment_up_left, dataComment.getUpname())
//                                    .text(R.id.tv_comment_down_right, RongDateUtils.getConversationFormatDate(sf.parse(dataComment.getCreateTime()).getTime(), FriendCircleInfoActivity.this))
////                                    .text(R.id.tv_comment_down_left, dataComment.getDepartName() + " " + dataComment.getPostName())
//                                    .text(R.id.tv_comment, dataComment.getMessage())
//                                    .clicked(R.id.iv_comment_comment, v -> {
//                                        clickedPos = slimAdapterExComment.getData().indexOf(dataComment);
//                                        parentId = dataComment.getId();
//                                        createId = dataComment.getCreater();
//                                        openInput(dataComment.getUpname(), "0");
//                                    })
//                                    .clicked(R.id.tv_comment, v -> {
//                                        clickedPos = slimAdapterExComment.getData().indexOf(dataComment);
//                                        parentId = dataComment.getId();
//                                        createId = dataComment.getCreater();
//                                        openInput(dataComment.getUpname(), "0");
//                                    })
//                                    .longClicked(R.id.tv_comment, v -> {
//                                        if (user.getGuid().equals(dataComment.getCreater())) {
//                                            alertView = ChooseAlertView.getBottomChoose(FriendCircleInfoActivity.this, "", new String[]{"复制", "删除"}, (o, position) -> {
//                                                switch (position) {
//                                                    case 1:
//                                                        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
//                                                        // 将文本内容放到系统剪贴板里。
//                                                        cm.setText(dataComment.getMessage());
//                                                        ToastUtil.showShort("复制成功");
//                                                        break;
//                                                    case 2:
//                                                        deleteComment = DialogUtil.tip(FriendCircleInfoActivity.this, "确认要删除该评论吗？", () -> {
//                                                            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.DELETE_FRIEND_CIRCLE_COMMENT)
//                                                                    .params("commentId", dataComment.getId())
//                                                                    .params("userGuid", user.getGuid())
//                                                                    .execute(new DialogCallBack<HttpResult>(FriendCircleInfoActivity.this) {
//                                                                        @Override
//                                                                        public void onSuccess(Response<HttpResult> response) {
//                                                                            HttpResult httpResult = response.body();
//                                                                            ToastUtil.showShort(httpResult.getMsg());
//                                                                            if (httpResult.getStatus() == 200) {
//                                                                                int deletePos = slimAdapterExComment.getData().indexOf(dataComment);
//                                                                                slimAdapterExComment.getData().remove(deletePos);
//                                                                                slimAdapterExComment.notifyItemRemoved(deletePos + 1);
//                                                                            }
//                                                                        }
//                                                                    });
//                                                        });
//                                                        deleteComment.show();
//                                                }
//                                            });
//                                            alertView.show();
//                                        } else {
//                                            alertViewDelete = ChooseAlertView.getBottomChoose(FriendCircleInfoActivity.this, "", new String[]{"复制"}, (o, position) -> {
//                                                switch (position) {
//                                                    case 1:
//                                                        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
//                                                        // 将文本内容放到系统剪贴板里。
//                                                        cm.setText(dataComment.getMessage());
//                                                        ToastUtil.showShort("复制成功");
//                                                        break;
//                                                    case 2:
//                                                        deleteComment = DialogUtil.tip(FriendCircleInfoActivity.this, "确认要删除该评论吗？", () -> {
//                                                            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.DELETE_FRIEND_CIRCLE_COMMENT)
//                                                                    .params("commentId", dataComment.getId())
//                                                                    .params("userGuid", user.getGuid())
//                                                                    .execute(new DialogCallBack<HttpResult>(FriendCircleInfoActivity.this) {
//                                                                        @Override
//                                                                        public void onSuccess(Response<HttpResult> response) {
//                                                                            HttpResult httpResult = response.body();
//                                                                            ToastUtil.showShort(httpResult.getMsg());
//                                                                            if (httpResult.getStatus() == 200) {
//                                                                                slimAdapterExComment.getData().remove(slimAdapterExComment.getData().indexOf(dataComment));
//                                                                                slimAdapterExComment.notifyItemRemoved(slimAdapterExComment.getData().indexOf(dataComment));
//                                                                            }
//                                                                        }
//                                                                    });
//                                                        });
//
//                                                        deleteComment.show();
//                                                        break;
//                                                }
//                                            });
//                                            alertViewDelete.show();
//                                        }
//                                        return true;
//                                    })
//                                    .clicked(R.id.iv_comment_head, view -> {
//                                        Bundle bundle = new Bundle();
//                                        bundle.putString(Config.GUID, dataComment.getCreater());
//                                        startActivity(UserCardActivity.class, bundle);
//                                    });
//                            ArrayList<CommentResultBean> commentResultBeen = new ArrayList<>();
//                            getListComment(dataComment, commentResultBeen);
//                            SlimAdapterEx slimAdapterExCommentComment = (SlimAdapterEx) ((RecyclerView) injector.findViewById(R.id.recyclerViewCommentComment)).getAdapter();
////                        if (slimAdapterExCommentComment == null) {
//                            ((RecyclerView) injector.findViewById(R.id.recyclerViewCommentComment)).setLayoutManager(new LinearLayoutManager(FriendCircleInfoActivity.this));
//                            slimAdapterExCommentComment = SlimAdapterEx.create().register(R.layout.list_item_37, new SlimInjector<CommentResultBean>() {
//                                @Override
//                                public void onInject(CommentResultBean data, IViewInjector injector) {
//                                    int start1 = 0;
//                                    int end1 = data.getUpname().length();
//                                    int start2 = end1 + 4;
////                                    int end2 = start2 + data.getBeforeName().length();
//                                    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.colorPrimary));
//                                    ForegroundColorSpan foregroundColorSpan1 = new ForegroundColorSpan(getResources().getColor(R.color.colorPrimary));
//                                    SpannableString spannableString = new SpannableString(data.getUpname() + " 回复 " + data.getBeforeName() + "说：" + data.getMessage());
//                                    spannableString.setSpan(foregroundColorSpan, start1, end1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//                                    spannableString.setSpan(foregroundColorSpan1, start2, end2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//                                    ClickableSpan clickableSpan = new ClickableSpan() {
//                                        @Override
//                                        public void onClick(View widget) {
//                                            Bundle bundle = new Bundle();
//                                            bundle.putString(Config.GUID, data.getCreater());
//                                            startActivity(UserCardActivity.class, bundle);
//                                            System.out.println("12");
//                                        }
//                                    };
//                                    ClickableSpan clickableSpan1 = new ClickableSpan() {
//                                        @Override
//                                        public void onClick(View widget) {
//                                            System.out.println("123");
//                                        }
//                                    };
//                                    spannableString.setSpan(clickableSpan, start1, end1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//                                    spannableString.setSpan(clickableSpan1, start2, end2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//                                    // 可以点击
//                                    ((TextView) injector.findViewById(R.id.tv_comment_comment)).setMovementMethod(CustomLinkMovementMethod.getInstance());
//                                    ActionSheetDialog alertViewInner = ChooseAlertView.getBottomChoose(FriendCircleInfoActivity.this, "", user.getGuid().equals(data.getCreater()) ? new String[]{"复制", "删除"} : new String[]{"复制"}, (o, position) -> {
//                                        switch (position) {
//                                            case 1:
//                                                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
//                                                // 将文本内容放到系统剪贴板里。
//                                                cm.setText(data.getMessage());
//                                                ToastUtil.showShort("复制成功");
//                                                break;
//                                            case 2:
//                                                deleteComment = DialogUtil.tip(FriendCircleInfoActivity.this, "确认要删除该评论吗？", () -> {
//                                                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.DELETE_FRIEND_CIRCLE_COMMENT)
//                                                            .params("commentId", data.getId())
//                                                            .params("userGuid", user.getGuid())
//                                                            .execute(new DialogCallBack<HttpResult>(FriendCircleInfoActivity.this) {
//                                                                @Override
//                                                                public void onSuccess(Response<HttpResult> response) {
//                                                                    HttpResult httpResult = response.body();
//                                                                    ToastUtil.showShort(httpResult.getMsg());
//                                                                    if (httpResult.getStatus() == 200) {
//                                                                        updateCommentByPosition(slimAdapterExComment.getData().indexOf(dataComment));
//                                                                    }
//                                                                }
//                                                            });
//                                                });
//                                                deleteComment.show();
//                                                break;
//                                        }
//                                    });
//                                    injector.text(R.id.tv_comment_comment, spannableString)
//                                            .clicked(R.id.tv_comment_comment, v -> {
//                                                clickedPos = slimAdapterExComment.getData().indexOf(dataComment);
//                                                parentId = data.getId();
//                                                openInput(data.getUpname(), data.getCreater());
//                                            })
//                                            .longClicked(R.id.tv_comment_comment, v -> {
//                                                alertViewInner.show();
////
//                                                return true;
//                                            });
//                                }
//                            }).attachTo((RecyclerView) injector.findViewById(R.id.recyclerViewCommentComment));
////                        }
//                            slimAdapterExCommentComment.updateData(commentResultBeen);
//                        } catch (ParseException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }).attachTo(dataBinding.recyclerViewComment);
//            }
//            RxView.clicks(dataBinding.llHeadImg)
//                    .subscribe((v) -> {
//                        Bundle bundle = new Bundle();
//                        bundle.putString("id", workBean.getId() + "");
//                        bundle.putString("idName", "friendCircleId");
//                        startActivity(ZanListActivity.class, bundle);
//                    });
//            RxView.clicks(dataBinding.ivOption)
//                    .subscribe((v) -> {
//                        if (deleteArticle == null) {
//                            deleteArticle = DialogUtil.tip(FriendCircleInfoActivity.this, "确认要删除该说说吗？", () -> {
//                                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.DELETE_FRIEND_CIRCLE)
//                                        .params("friendCircleId", workBean.getId())
//                                        .params("userGuid", user.getGuid())
//                                        .execute(new DialogCallBack<HttpResult>(FriendCircleInfoActivity.this) {
//                                            @Override
//                                            public void onSuccess(Response<HttpResult> response) {
//                                                HttpResult httpResult = response.body();
//                                                ToastUtil.showShort(httpResult.getMsg());
//                                                if (httpResult.getStatus() == 200) {
//                                                    setResult(Config.DELETE);
//                                                    finish();
//                                                }
//                                            }
//                                        });
//                            });
//                        }
//                        deleteArticle.show();
//                    });
//            updateView();
//            mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
//            dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
//                @Override
//                public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
//                    if (page == 1) {
//                        setView();
//                    }
//                    return OkGo.<HttpResult<List<CommentResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.FRIEND_CIRCLE_COMMENT)
//                            .params("friendCircleId", workBean.getId())
//                            .params("id", page == 1 ? 0 : slimAdapterExComment.getDataItem(slimAdapterExComment.getData().size() - 1).getId())
//                            .converter(new JsonConverter<HttpResult<List<CommentResultBean>>>() {
//                                @Override
//                                public void test() {
//                                    super.test();
//                                }
//                            })
//                            .adapt(new FlowableBody<HttpResult<List<CommentResultBean>>>())
//                            .flatMap(new Function<HttpResult<List<CommentResultBean>>, Publisher<List<CommentResultBean>>>() {
//                                @Override
//                                public Publisher<List<CommentResultBean>> apply(@NonNull HttpResult<List<CommentResultBean>> listHttpResult) throws Exception {
//                                    return Flowable.just(listHttpResult.getItems() == null ? new ArrayList<CommentResultBean>() : listHttpResult.getItems());
//                                }
//                            });
//                }
//            }, 10);
//            dataBinding.rlHeadRoot.removeViewAt(0);
//            slimAdapterExComment.addHeader(dataBinding.nestedScrollView);
//            slimAdapterExComment.updateData(new ArrayList());
//            mvcHelper.setDataSource(dataSource);
//            mvcHelper.setAdapter(slimAdapterExComment);
////            RefreshUtil.openRefresh(dataBinding.coolRefreshView, coolRefreshView -> {
////            });
//            mvcHelper.refresh();
//        }


    }

    private void zan() {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_FRIEND_CIRCLE_ZAN)
                .params("companyId", user.getCompanyId())
                .params("userGuid", user.getGuid())
                .params("friendCircleId", workBean.getId())
                .execute(new JsonCallBack<HttpResult>() {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                        }
                    }
                });

        isChange = true;
        dataBinding.stvSupport.setText((workBean.getZanCount() + 1) + "");
        dataBinding.stvSupport.setDrawable(getResources().getDrawable(R.mipmap.ic_heart_red));
        workBean.setZanCount(workBean.getZanCount() + 1);
        workBean.setIsZan(true);
        dataBinding.stvSupport.setEnabled(false);
        workBean.getUserZan().add(0, new WorkCircleBean.UserZanBean(user.getGuid(), user.getIphoto()));
        slimAdapterExHead.updateData(workBean.getUserZan().subList(0, workBean.getUserZan().size() > 3 ? 3 : workBean.getUserZan().size()));
    }

    private void updateCommentByPosition(int pos) {
        OkGo.<HttpResult<CommentResultBean>>post(HttpConfig.BASE_URL + HttpConfig.FRIEND_CIRCLE_COMMENT_BYID)
                .params("commentId", slimAdapterExComment.getDataItem(pos).getId())
                .execute(new DialogCallBack<HttpResult<CommentResultBean>>(FriendCircleInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<CommentResultBean>> response) {
                        HttpResult<CommentResultBean> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
//                            slimAdapterExComment.getData().get(pos).setChildren(httpResult.getItems().getChildren());
                            slimAdapterExComment.notifyItemChanged(pos + 1);
                        }
                    }
                });
    }

    private void updateView() {
        ImageUtil.setCircleImage(workBean.getIphoto(), dataBinding.ivHead);
        dataBinding.tvCity.setText(workBean.getLocation());
        dataBinding.tvCity.setVisibility(TextUtils.isEmpty(workBean.getLocation()) ? View.GONE : View.VISIBLE);
        try {
            dataBinding.tvTime.setText(RongDateUtils.getConversationListFormatDate(sf.parse(workBean.getCreateTime()).getTime(), this));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        dataBinding.ivOption.setVisibility(user.getGuid().equals(workBean.getCreater()) ? View.VISIBLE : View.GONE);
        dataBinding.stvName.setText(workBean.getUpname());
        dataBinding.tvContent.setText(workBean.getMessage());
        dataBinding.stvSupport.setText(workBean.getZanCount() + "");
        dataBinding.stvSupport.setDrawable(getResources().getDrawable(workBean.isIsZan() ? R.mipmap.ic_heart_red : R.mipmap.ic_heart_hint));
        dataBinding.stvMsg.setText(workBean.getCommentCount() + "");
        dataBinding.stvMsg.setDrawable(getResources().getDrawable(R.mipmap.ic_comment));
        slimAdapterExImg.updateData(workBean.getNewImages());
        dataBinding.stvSupport.setEnabled(!workBean.isIsZan());
        slimAdapterExHead.updateData(workBean.getUserZan().
                subList(0, workBean.getUserZan().size() > 3 ? 3 : workBean.getUserZan().size()));
//        slimAdapterExComment.updateData(commentBeans);
    }

    private void setRecyclerViewMoveFast(RecyclerView.LayoutManager layoutManager, RecyclerView recyclerView) {
        recyclerView.setLayoutManager(layoutManager);
//        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
//        recyclerView.setNestedScrollingEnabled(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onBackPressed() {

        if (dataBinding.vImageWatcher.isShown()) {
            dataBinding.vImageWatcher.onSingleTapConfirmed();
        } else {
            if (isChange) {
                Intent intent = new Intent();
                intent.putExtra("bean", workBean);
                setResult(Config.CHANGE, intent);
            }
            super.onBackPressed();
        }
    }

//    public void getListComment(CommentResultBean commentResultBean, List<CommentResultBean> list) {
//        if (commentResultBean.getChildren() == null || commentResultBean.getChildren().size() == 0) {
//            return;
//        } else {
//            for (int i = 0; i < commentResultBean.getChildren().size(); i++) {
//                list.add(commentResultBean.getChildren().get(i));
//                getListComment(commentResultBean.getChildren().get(i), list);
//            }
//        }
//    }

    @Override
    public void setView() {
//        Flowable<HttpResult<List<CommentResultBean>>> flowComment = OkGo.<HttpResult<List<CommentResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.WORK_CIRCLE_COMMENT_LIST)
//                .params("workCircleId", workBean.getId())
//                .params("id", 0)
//                .converter(new JsonConverter<HttpResult<List<CommentResultBean>>>() {
//                    @Override
//                    public void test() {
//                        super.test();
//                    }
//                })
//                .adapt(new FlowableBody<HttpResult<List<CommentResultBean>>>());
//        Flowable<HttpResult<WorkCircleBean>> flowWorkInfo = OkGo.<HttpResult<WorkCircleBean>>post(HttpConfig.BASE_URL + HttpConfig.WORK_CIRCLE_INFO)
//                .params("workCircleId", workBean.getId())
//                .params("userGuid", user.getGuid())
//                .converter(new JsonConverter<HttpResult<WorkCircleBean>>() {
//                    @Override
//                    public void test() {
//                        super.test();
//                    }
//                })
//                .adapt(new FlowableBody<HttpResult<WorkCircleBean>>());
//        Flowable.combineLatest(flowWorkInfo, flowComment, (BiFunction<HttpResult<WorkCircleBean>, HttpResult<List<CommentResultBean>>, Object>) (workCircleBeanHttpResult, listHttpResult) -> {
//            workBean = workCircleBeanHttpResult.getItems();
//            commentBeans = listHttpResult.getItems();
//            return 1;
//        })
//                .subscribe(
//                        v -> {
//                            updateView();
//                            dataBinding.coolRefreshView.setRefreshing(false);
//                        }
//                );
//        dataBinding.appbarLayout.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
//            if (verticalOffset >= 0) {
//                dataBinding.coolRefreshView.setEnabled(true);
//            } else {
//                dataBinding.coolRefreshView.setEnabled(false);
//            }
//        });
        OkGo.<HttpResult<WorkCircleBean>>post(HttpConfig.BASE_URL + HttpConfig.FRIEND_CIRCLE_INFO)
                .params("friendCircleId", workBean.getId())
                .params("userGuid", user.getGuid())
                .execute(new DialogCallBack<HttpResult<WorkCircleBean>>(FriendCircleInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<WorkCircleBean>> response) {
                        HttpResult<WorkCircleBean> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            workBean = httpResult.getItems();
                            updateView();
                        }
                    }
                });
    }

    private void refreshComment() {
        OkGo.<HttpResult<List<CommentResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.FRIEND_CIRCLE_COMMENT)
                .params("friendCircleId", workBean.getId())
                .params("id", 0)
                .execute(new DialogCallBack<HttpResult<List<CommentResultBean>>>(FriendCircleInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<CommentResultBean>>> response) {
                        HttpResult<List<CommentResultBean>> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            slimAdapterExComment.updateData(httpResult.getItems());
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_msg:
                parentId = 0;
                openInput(workBean.getUpname(), "0");
                break;
        }
    }

    private void openInput(String replyName, String replyGuid) {
        if (user.getGuid().equals(replyGuid)) {
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString("hint", "回复" + replyName + ":");
        inputDialogFragment.setArguments(bundle);
        inputDialogFragment.show(getSupportFragmentManager(), "input");
    }

    private int parentId = 0;
    private boolean isChange = false;

    private void initInputDialog() {
        inputDialogFragment = new InputDialogFragment();
        inputDialogFragment.setOnClickListener(s -> {
                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_FRIEND_CIRCLE_COMMENT)
                            .params("userGuid", user.getGuid())
                            .params("message", s)
                            .params("friendCircleId", workBean.getId())
                            .params("parentId", parentId)
                            .execute(new DialogCallBack<HttpResult>(this) {
                                @Override
                                public void onSuccess(Response<HttpResult> response) {
                                    HttpResult httpResult = response.body();
                                    ToastUtil.showShort(httpResult.getMsg());
                                    if (httpResult.getStatus() == 200) {
                                        inputDialogFragment.dismiss();
                                        if (parentId == 0) {
                                            refreshComment();
                                        } else {
                                            updateCommentByPosition(clickedPos);
                                        }
                                        workBean.setCommentCount(workBean.getCommentCount() + 1);
                                        dataBinding.stvMsg.setText(workBean.getCommentCount() + "");
                                        isChange = true;
                                    }
                                }
                            });
                }
        );
    }
}
